package com.example.demo.module.repository.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.enums.ProductRepositoryStateEnum;
import com.example.demo.common.entity.enums.RepositoryTypeEnum;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.repository.MaterialPoJo;
import com.example.demo.entity.pojo.repository.ProductRepositoryPoJo;
import com.example.demo.entity.pojo.repository.RepositoryPoJo;
import com.example.demo.entity.request.repository.ProductRepositoryRequest;
import com.example.demo.module.repository.mapper.ProductRepositoryMapper;
import com.example.demo.module.repository.service.MaterialService;
import com.example.demo.module.repository.service.ProductRepositoryService;
import com.example.demo.module.repository.service.RepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;


/**
 * .
 *
 * @author Mei Ruoxiao
 * @since 2020/9/18
 */
@Slf4j
@Service
public class ProductRepositoryServiceImpl extends BaseServiceImpl<ProductRepositoryMapper, ProductRepositoryPoJo> implements ProductRepositoryService {
    @Resource
    private MaterialService materialService;
    @Resource
    private RepositoryService repositoryService;


    @Override
    public boolean warehouse(ProductRepositoryRequest request, long companyId) {
        log.info("warehouse:[request:{}, companyId:{}]", request, companyId);
        MaterialPoJo materialPoJo = new MaterialPoJo();
        BeanUtils.copyProperties(request, materialPoJo);
        //获取该企业的默认成品仓库文件夹
        RepositoryPoJo repositoryPoJo = repositoryService.getRootFolder(RepositoryTypeEnum.PRODUCT.value(), companyId);
        materialPoJo.setRepositoryId(repositoryPoJo.getId());
        materialPoJo.setType(RepositoryTypeEnum.PRODUCT.value());
        ProductRepositoryPoJo poJo = getById(request.getId());
        //判断成品仓库是否存在该成品
        MaterialPoJo find = materialService.getOne(request.getCode());
        if (Objects.nonNull(find)) {
            find.setStock(find.getStock() + request.getStock());
            //设置已入库数量
            poJo.setStock(poJo.getStock() + request.getStock());
        } else {
            materialService.create(materialPoJo);
            //设置已入库数量
            poJo.setStock(request.getStock());
        }
        //设置产品入库状态
        if (Objects.equals(poJo.getStock(), request.getQuantity())) {
            poJo.setState(ProductRepositoryStateEnum.FINISH.value());
        }
        return updateById(poJo);
    }

    @Override
    public Page<ProductRepositoryPoJo> page(Integer state, Date start, Date end, int page, int size, long companyId) {
        log.info("page:[state:{}, start:{}, end:{}, page:{}, size:{}, companyId:{}]", state, start, end, page, size, companyId);
        return page(Wrappers.<ProductRepositoryPoJo>lambdaQuery()
                        .eq(Objects.nonNull(state), ProductRepositoryPoJo::getState, state)
                        .ge(Objects.nonNull(start), ProductRepositoryPoJo::getStartOperationTime, start)
                        .le(Objects.nonNull(end), ProductRepositoryPoJo::getStartOperationTime, end)
                        .eq(ProductRepositoryPoJo::getCompanyId, companyId),
                page,
                size,
                OrderItem.desc("create_time")
        );
    }
}